﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Project5188.BUS;
using Project5188.BUS.SalesAndCustomerRelations;
using Project5188.Client.ClientCodes;
using Project5188.Client.SalesAndCustomerRelations.Reports;

namespace Project5188.Client.SalesAndCustomerRelations
{
    /// <summary>
    /// Interaction logic for CreateOfferPage.xaml
    /// </summary>
    public partial class CreateOfferPage
    {
        private CollectionViewSource _resources;

        public CreateOfferPage()
            : base(Extensions.PageNames.CreateOfferPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgCreateOffers.ItemsSource = _resources.Source as IEnumerable;
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtCompanyNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<offer> offerDetail = new Offers();
            insert.SetEntity(offerDetail, Extensions.PageNames.CreateOfferPage.ToString());
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var offers = new Offers();
                _resources = Resources["crateOfferViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    offers.GetEntityListByParameters(txtCompanyNo.Text, txtCompanyName.Text, dtContentDate.SelectedDate,
                                                     txtAuthorsOffer.Text), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCreateOffers.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var offers = new Offers();
                _resources = Resources["crateOfferViewSource"] as CollectionViewSource;
                pagingControl.SetDataSource(
                    offers.GetEntityById(TransactionInfo.TransactionId.Value), 1, 10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCreateOffers.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var offerValue = (offer)dgCreateOffers.SelectedItem;
            var detail = new DetailWindow();
            DetailView<offer> offerDetail = new Offers { Entity = offerValue };
            detail.SetEntity(offerDetail, Extensions.PageNames.CreateOfferPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var offerValue = (offer)dgCreateOffers.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<offer> offerDetail = new Offers();
            update.SetEntity(offerDetail, Extensions.PageNames.CreateOfferPage.ToString(), offerValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var offerValue = (offer)dgCreateOffers.SelectedItem;
            DetailView<offer> offerDetail = new Offers();
            GenerateDeleteDialog(offerDetail, offerValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            var offerValue = (offer) dgCreateOffers.SelectedItem;
            var report = new OfferReportWindow(offerValue.a_ID);
            report.Show();
        }
    }
}
